﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ModelLayer;

namespace ViewLayer.MessageConfiguration
{
    public partial class MessageConfigurationForm : Form
    {
        MedAgendaEntities entities = new MedAgendaEntities();

        public MessageConfigurationForm()
        {
            InitializeComponent();
            var message = (from m in entities.Message_
                           select m).SingleOrDefault();
            if (message == null)    
            {
                cmbOptions.SelectedIndex = 0;
            }
            else
            {
                cmbOptions.SelectedItem = message.Option_;
                txtMessage.Text = message.Message_1;
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (cmbOptions.SelectedIndex == 0)
            {
                MessageBox.Show(null, "Seleccione un opción de envío.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                 var message = (from m in entities.Message_
                           select m).SingleOrDefault();
                 if (message == null)
                 {
                     Message_ m = new Message_();
                     m.OID_ = Guid.NewGuid();
                     m.Message_1 = txtMessage.Text;
                     m.Option_ = cmbOptions.SelectedItem.ToString();
                     entities.Message_.AddObject(m);
                 }
                 else
                 {
                     message.Message_1 = txtMessage.Text;
                     message.Option_ = cmbOptions.SelectedItem.ToString();
                 }
                 entities.SaveChanges();
                 MessageBox.Show(null, "El mensaje predeterminado ha sido guardado", "Actualización", MessageBoxButtons.OK, MessageBoxIcon.Information);
                 this.Close();
            }
        }
    }
}
